import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	cookie: {
		uk: string;
		[key: string]: string
	};
	urls: {
		base: string;
		origin: string;
		url: string;
	};
	query: {};
	params: {};
	headers: {};
	captcha: string;
	attr_no: string;
	pn: number;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
// 表名	user_defined_attr_values
// 标题	自定义属性可选值
// 字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
// _id	id	string	50	ⅹ	√
// productid	产品ID	string	50	√	ⅹ
// priority	排序码	string	32	√	ⅹ
// attr_no	属性编号	string	32	√	ⅹ
// optional_value_no	可选值编号	string	32	√	ⅹ
// optional_value	可选值	string	64	√	ⅹ

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:e:\考核-2\kh\src\device-management-ygh\zj-000005\s001,action_id:' + action_id);
	const q = query(action_id, session_id, systemid);
	q.prepare('user_defined_attr_values', ['optional_value_no', 'optional_value'], { productid, attr_no: message.attr_no }, 200, 1, [], []);
	const [res] = await q.exec();

	const d = res.map((value) => {
		// .{..value}是分别把value的值放到js对象里面 === {a:'a',b:'b'}
		// v是一个string v的值是的每一条表中的字段名及值
		return { ...value, v: JSON.stringify(value) };
	});
	log('Service end path:e:\考核-2\kh\src\device-management-ygh\zj-000005\s001,action_id:' + action_id);
	return {
		data: { res: d }
	};
}
